#include<stdio.h>
#include<string.h>
#include<stdlib.h>


int *array,*array2,*array3;
int size,size2,size3;

void memoryTest(int *array)
{
	if (array == NULL)
	{
		printf ("Error: Failed to allocate memory!\n");
		printf ("Press any key to terminate the program...\n");
		exit(1);
	}
}


int *append (int num)
{
	array = (int *) realloc (array,sizeof(int) * size);
	memoryTest(array);
	array[size-1] = num;
	return array;
}


int *extend()
{
	int i,j,k;	
	size3=size+size2;
	array3 = (int *) malloc (sizeof(int) * size3);
	memoryTest(array3);
	for(i=0;i<size;i++)
	{
		array3[i]=array[i];
	}
	for(j=size,k=0;j<size3,k<size2;j++,k++)
	{
		array3[j]=array2[k];
	}
	return array3;
}
	





int pop(int num)
{
	int i,tmp=0;
	int flag;
	for (i=0;i<size+1;i++)
	{
		if (array[i] == array[num])
		tmp = 1;
	}
	if (tmp == 0)
	{
		//pop last item of array
	}
	tmp = 0;
	for (i=0;i<size;i++)
	{
		if (array[i] == array[num])
		{
			flag=array[i];
			tmp = array[i+1];
			array[i+1] = array[i];
			array[i] = tmp;
		}
	}
	array = (int *) realloc (array,sizeof(int) * size);
	memoryTest(array);
	return flag;
}

void display()
{
	int i;
	putchar ('\n');
	printf ("***** PRINTING ARRAY *****\n");
	putchar ('\n');
	for (i=0;i<size;i++)
	{
		printf ("%d ",array[i]);
		//putchar('\n');
	}
}

int*sort()
{
	int i,j,temp;
	for(i=0;i<size;i++)
		for(j=0;j<size-1;j++)
			if(array[j]>array[j+1])
			{
				temp=array[j];
				array[j]=array[j+1];
				array[j+1]=temp;
			}
	return array;
}	

int *reverse()
{
	int i,j,temp;
	j=size;
   	for(i=0;i<--j;i++)
   	{
      		temp=array[i];
      		array[i]=array[j];
		array[j]=temp; 
  	}
	return array;
}

int count(int num)
{
	int i,count;
	count=0;
	for(i=0;i<size;i++)
	{
		if(num==array[i])
			count++;
	}
	return count;
}

int indexed(int num)
{
	int i;
	for(i=0;i<size;i++)
	{
		if(num==array[i])
			return i;
	}
	printf("Error \n");
	return 0;
}

int *delete (int num)
{
	int i,tmp=0;
	for (i=0;i<size+1;i++)
	{
		if (array[i] == num)
		tmp = 1;
	}
	if (tmp == 0 || array == NULL)
	{
		printf ("Error: No such number in array!\n");
		printf ("Press any key to return into the main menu...\n");
		return array;
	}
	tmp = 0;
	for (i=0;i<size;i++)
	{
		if (array[i] == num)
		{
			tmp = array[i+1];
			array[i+1] = array[i];
			array[i] = tmp;
		}
	}
	array = (int *) realloc (array,sizeof(int) * size);
	memoryTest(array);
	return array;
}

int*insert(int pos,int num)
{
	int i,j,m;
	for(i=0;i<size;i++)
	{
		if (i==pos)
		{
			pos=i;
			break;
		}
	}
	m=size-pos+1;
	for(j=0;j<=m;j++)
	{
		array[size-j+2]=array[size-i+1];
	}
	array[pos]=num;
	array = (int *) realloc (array,sizeof(int) * size);
	memoryTest(array);
	return array;
}
	

int main()
{
	int num,pos;
	int choice;
	int flag=1;
	while (choice != 10)
	{
		printf ("***** MAIN MENU *****\n\n");
		printf ("1. Append number.\n");
		printf ("2. Delete number.\n");
		printf ("3. Display array.\n");
		printf ("4. Sort array.\n");
		printf ("5. Reverse array.\n");
		printf ("6. Count of a no. in array.\n");
		printf ("7. Index of a no. in array.\n");
		printf ("8. Pop a no. in array.\n");
		printf ("9. Extend List.\n");
		printf("10. Insert a no. in array.\n");
		printf ("11. Quit.\n");
		putchar ('\n');
		printf ("Enter your choice: ");
		scanf ("%d",&choice);
		switch(choice)
		{
			case 1: printf ("Add number: ");
				scanf ("%d",&num);
				size++;
				array = append (num);
				break;
			case 2: printf ("Erase number: ");
				scanf ("%d",&num);
				size--;
				array = delete (num);
				break;
			case 3: display();
				break;
			case 4: array = sort ();
				break;
			case 5: array = reverse ();
				break;
			case 6: printf ("Enter the no.: ");
				scanf ("%d",&num);
				num=count (num);
				printf("%d",num);
				break;
			case 7: printf ("Enter the no.: ");
				scanf ("%d",&num);
				num=indexed (num);
				printf("%d",num);
				break;
			case 8: printf ("Enter the no.: ");
				scanf ("%d",&num);
				num=pop (num);
				printf("%d",num);
				break;
			case 9: while(flag!=0)
				{						
					printf ("Add number to the new list u wanna concatenate with the original one: (press 1 when u r done)");
					scanf ("%d",&num);
					size2++;
					array2 = append (num);
					printf("do u wanna add more elements(1 for yes,0 for no) :");
					scanf("%d",&flag);
				}
				array3=extend(array,array2);
				break;
			case 10: printf("\n Enter the position where u wanna insert ur element : ");
				 scanf("%d",&pos);
				 printf("\n Enter the element u wanna insert : ");
				 scanf("%d",&num);
				 array=insert(pos,num);
				 break;
			case 11: printf ("Press any key to quit...\n");
				break;
			default:printf ("Error: Wrong choice !\n");	
				printf ("Press any key to return into the main menu...\n");
				break;
		}
	}
	free (array);
	return 0;
}




